package com.yufei.infoExtractor.extractor.propertyExtractor;

import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.yufei.infoExtractor.core.LinkAction;
import com.yufei.infoExtractor.extractor.ContentExtractor;
import com.yufei.infoExtractor.extractor.ExtractorAction;
import com.yufei.infoExtractor.extractor.PropertyExtractor;
import com.yufei.infoExtractor.pfw.entity.PropertyMatch;
import com.yufei.infoExtractor.pfw.entity.Relatedlink;
import com.yufei.infoExtractor.util.AppUtil;

/**
 * created by @author jasstion at 2012-10-29
 * 用于某些集合属性的提取处理（比如：某些新闻中的评论信息）
 * 
 */
public class CollectionPropertyExtractor  extends PropertyExtractor implements ExtractorAction{
	private static final Log mLog = LogFactory.getLog(CollectionPropertyExtractor.class);

	@Override
	public void extract(Map context) {
		// TODO Auto-generated method stub
		PropertyMatch propertyMatch=(PropertyMatch) context.get("currentPropertyMatch");
		Relatedlink link = (Relatedlink) context.get("currentLink");
		String htmlContent = (String) context.get("currentHtmlContent");
		String fieldName = propertyMatch.getProperty();
		// common property can set value simple by regex
		// set properties for Collection type property
		// collection type property regex:匹配分页连接的正则表达式：匹配集合中的元素的正则表达式
		// 目前仅仅支持集合元素类型为String
		String fieldRegex=propertyMatch.getMatch();
		String pageUrlRegex = null;
   
		
		pageUrlRegex = fieldRegex
				.substring(fieldRegex.indexOf(":") + 1);
		context.put("pageUrlRegex", pageUrlRegex);
		context.put("currentCollectionName", fieldName);
		context.put("currentContentRegex",
				fieldRegex.substring(0, fieldRegex.indexOf(":")));
		try {
			AppUtil.getLinkAction(
					String.valueOf(LinkAction.LINKTYPE_PAGINATION))
					.execute(link, context);

		} catch (Exception e) {
			// TODO Auto-generated catch block

			mLog.debug("action for the " + fieldName
					+ " pagination wrong!" + "the exception is "
					+ e.getMessage());
		}
	}

}
